package com.train.classeval.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.train.classeval.entity.IndexOV;
import com.train.classeval.entity.LoginRequest;
import com.train.classeval.entity.Menu;
import com.train.classeval.entity.User;
import com.train.classeval.service.impl.MenuServiceImpl;
import com.train.classeval.service.impl.UserServiceImpl;
import com.train.classeval.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs
 * @since 2021-09-11
 */
@RestController
@CrossOrigin
public class UserController {

//    @RequestMapping("/user/{id}")
//    public Object user(@PathVariable("id") Long id){
//        User user = new User();
//        user.setUserId(id);
//        user.setRealName("张三丰");
//
//        return user;
//    }

    @Autowired
    UserServiceImpl userService;

    @Autowired
    private MenuServiceImpl menuService;



    /**
     * 查看所有用户
     * @return
     */
    @GetMapping("/users")
    public JsonData updateUser(){
        List<User> list = userService.findAllUser();
        if (list != null) {
            return JsonData.BuildSuccess(list,"查看用户成功!");
        }
        return JsonData.BuildError("查看用户失败");
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public JsonData addUser(@RequestBody User user) {
        //初始化相关数据
        user.setSalt("thereisnowaytodie6699");
        user.setCreateDate(LocalDateTime.now());
        user.setUpdateDate(LocalDateTime.now());
        user.setImgPath("https://img2.woyaogexing.com/2021/10/04/fdd5c19fd39e446ea97ed8c586fb417c!");
        user.setStatus(0);
        boolean save = userService.save(user);
        if (save == true) {
            return JsonData.BuildSuccess("添加用户成功!");
        }
        return JsonData.BuildError("添加用户失败!");
    }


    /**
     * 登录
     * @param loginRequest
     * @return
     */
    @PostMapping("/login")
    public JsonData loginUser(@RequestBody LoginRequest loginRequest){
        final User user = userService.loginUser(loginRequest.getUserName());
        if(user != null){
            if(user.getPassword().equals(loginRequest.getPassword())){
                return JsonData.BuildSuccess(user,"登录成功!");
            }
        }
        return JsonData.BuildError("账号或密码错误!");
    }

    /**
     * 修改密码
     * @param user
     * @return
     */
    @PutMapping("/update_password")
    public JsonData updatePassword(@RequestBody User user) {
        final int i = userService.updatePassword(user);
        if (i == 1) {
            return JsonData.BuildSuccess("修改用户密码成功!");
        }
        return JsonData.BuildError("修改用户密码失败!");
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @DeleteMapping("/deleteUser/{uid}")
    public JsonData deleteUser(@PathVariable("uid") Integer uid) {
        boolean b = userService.removeById(uid);
        if (b == true) {
            return JsonData.BuildSuccess("删除用户成功!");
        }
        return JsonData.BuildError("删除用户失败!");
    }

    /**
     * 查询用户详情
     * @param uid
     * @return
     */
    @GetMapping("/findOneUser/{uid}")
    public JsonData findOneUser(@PathVariable("uid") Integer uid) {
         User user = userService.getById(uid);
        if (user != null) {
            return JsonData.BuildSuccess(user, "查询信息成功!");
        }
        return JsonData.BuildError("查询失败!");
    }
}


